package com.example.demo.controller;

import com.example.demo.util.PospVerifyUtils;
import com.example.demo.util.RSAUtils;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class TestController {
    @RequestMapping(value = "/getSignPost", method = RequestMethod.POST)
    public Object getSign(HttpServletRequest request, @RequestBody CreateCouponRequestEntity createCouponRequestEntity) throws Exception{
        String MCMS_DATE = "MCMS_DATE";
        String PARTNER_ID = "PARTNER_ID";
        String SIGN_TYPE = "SIGN_TYPE";
        String partnerId = request.getHeader(PARTNER_ID);
        String mcmsDate = request.getHeader(MCMS_DATE);
        String signType = request.getHeader(SIGN_TYPE);
        String keyEntity = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCD5RdXQJcq1EgzOrLPesOV4KnddoeZUXpMJekmmZSsf2UhPrwpLR5sD0YKxpXK7cc0/u+xhwqFl89BGXeQRTjkdgB0EMuXKZweTGORW2ZJP4N1560cG+0906jdOk8AAjDRiVOMO64D5dOMp/1gE9tc1ujNayFsD7lnlHc30wYJw+9UGX232zAsCPhQ/GWBxQZFo9ou5QGD7P0B+8MD0KdAloKC5DdVRSleZRxNbFQnaX/PEo5NP2mNEXdd2n50RqYD7Z87/VL3VK4JqQ1eYmM+h4lvROvVOozstrvjl/bIgcJMjXulsVAHrX51HxGG+i1X0494jImLgWZGh7bfUksxAgMBAAECggEAM10Z896xf/pfi6PGUspqNInjXN11/R0f1VvGKkQRBAQwq/u0ZwytFkP0cOnZF8f1lgvZ6HbAgDoYei+iwkpS15Wko46O2nVX2OzCsa64bLoRRe9NGO8/Op6c2lqEvWoc/NzP7oKnR9lOwy4AnFPfT45Z5vVbdKvC9jV5IAJMDazt5DI6Dtclb9bQIwvThi5w76e5sjzsmTeAiTgd2k9uyNGq+QwIVU97TiVeJdWv1ze0n/lLTrRmjyXg4WW07zLxa83zak6tHVLbh3mA/qIYu5v3UzOyIsDpiGuJ4IHdVr2sokgx99aAWHr1DvYcODJG+qhrgA2a4EpY4EFlPvseqQKBgQDvzln0+cGjQ4jmMtjX8iB1ZNq+T5d0XnLbpMNG6AT+OV4w2eLpvzfxjSvFemJlR/nHjxiDFPmjrpnAXyFrRphWoJn5cxBNzKfN6YnKylPAFoYUea5uTpv4xkhISiW3kcYlOsTErWdp49wNi4IlYd7PP9eJl4WiW5rX9xYakZd3ewKBgQCMzTmLGV2nuFS9LisHsXI4qxhqwMOAm1XNV0aoQkKIELRRogim0xl4He5T4iWAFhFA47JFt9L4cpg2q9g02huEQvl16puJpn4qJc8hmLNOLihOT1TBtYgq5WcQ6xPcYvpAKrYyH/Heclgn+JtizazweGd9IzzWjrefCQbKQogyQwKBgDL2v9d4XNa9u5Jwq/T42D0augJIF//7Mp3LXlTramQ8/O+U3syeft0vC2ZkpBSyPztSBfuVsZg/ux1PGmMaFbYTW6XP9l8SZ3czcr1OUo44OzaOxatbuXodaa8zUuC4ckvnOXozAA9ZBaM7SuV270tMXho3wYbHwRi0wlXTK0WRAoGAcfe/pxa+Fv7+IC7Zh52a3cGnvukOle+l0NLfD1yqq6YRvq3QIPBoAjD/8wfu4LF7/7EOYJ49dvqSuFX6aDVO5oyrjUV75jxMjOMTuJb4M8TqvRA7g5vjfw02kpwk9FNtSjkIRqRSy4J0Vs5FQAFJBVAAPGskWGsmt+S/iUG3Ki8CgYAD+8j0gSopXb2bzjF/3rcDasf+Nix89Tv6xjuaT4BpB/hwkPyI0Jvwm6JfF+GzzXKJqLXqOQ4dl8iycKjezvyInaMUudqAKVLrcURVfyJmtUbNdMfRVtDEf/o5+6nKaBPkc5hmbL796jJes/fM8MORmvRTEYaxvAHqQnS5klvFWQ==";
        Map<String, String> otherMap = new HashMap<>();
        otherMap.put(PARTNER_ID, partnerId);
        otherMap.put(MCMS_DATE, mcmsDate);
        otherMap.put(SIGN_TYPE, signType);
        String encryptionData = PospVerifyUtils.getEncryptionDate(createCouponRequestEntity, request.getQueryString(), otherMap);
        System.out.println(encryptionData);
        return Base64.encodeBase64String(RSAUtils.sign(RSAUtils.loadPrivateKey(keyEntity), encryptionData.getBytes("UTF-8"), RSAUtils.SIGNATURE_ALGORITHM_SHA256WITHRSA));
    }

    @RequestMapping(value = "/getSignGet", method = RequestMethod.GET)
    public Object getSign(HttpServletRequest request) throws Exception{
        String MCMS_DATE = "MCMS_DATE";
        String PARTNER_ID = "PARTNER_ID";
        String SIGN_TYPE = "SIGN_TYPE";
        String partnerId = request.getHeader(PARTNER_ID);
        String mcmsDate = request.getHeader(MCMS_DATE);
        String signType = request.getHeader(SIGN_TYPE);
        String keyEntity = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCD5RdXQJcq1EgzOrLPesOV4KnddoeZUXpMJekmmZSsf2UhPrwpLR5sD0YKxpXK7cc0/u+xhwqFl89BGXeQRTjkdgB0EMuXKZweTGORW2ZJP4N1560cG+0906jdOk8AAjDRiVOMO64D5dOMp/1gE9tc1ujNayFsD7lnlHc30wYJw+9UGX232zAsCPhQ/GWBxQZFo9ou5QGD7P0B+8MD0KdAloKC5DdVRSleZRxNbFQnaX/PEo5NP2mNEXdd2n50RqYD7Z87/VL3VK4JqQ1eYmM+h4lvROvVOozstrvjl/bIgcJMjXulsVAHrX51HxGG+i1X0494jImLgWZGh7bfUksxAgMBAAECggEAM10Z896xf/pfi6PGUspqNInjXN11/R0f1VvGKkQRBAQwq/u0ZwytFkP0cOnZF8f1lgvZ6HbAgDoYei+iwkpS15Wko46O2nVX2OzCsa64bLoRRe9NGO8/Op6c2lqEvWoc/NzP7oKnR9lOwy4AnFPfT45Z5vVbdKvC9jV5IAJMDazt5DI6Dtclb9bQIwvThi5w76e5sjzsmTeAiTgd2k9uyNGq+QwIVU97TiVeJdWv1ze0n/lLTrRmjyXg4WW07zLxa83zak6tHVLbh3mA/qIYu5v3UzOyIsDpiGuJ4IHdVr2sokgx99aAWHr1DvYcODJG+qhrgA2a4EpY4EFlPvseqQKBgQDvzln0+cGjQ4jmMtjX8iB1ZNq+T5d0XnLbpMNG6AT+OV4w2eLpvzfxjSvFemJlR/nHjxiDFPmjrpnAXyFrRphWoJn5cxBNzKfN6YnKylPAFoYUea5uTpv4xkhISiW3kcYlOsTErWdp49wNi4IlYd7PP9eJl4WiW5rX9xYakZd3ewKBgQCMzTmLGV2nuFS9LisHsXI4qxhqwMOAm1XNV0aoQkKIELRRogim0xl4He5T4iWAFhFA47JFt9L4cpg2q9g02huEQvl16puJpn4qJc8hmLNOLihOT1TBtYgq5WcQ6xPcYvpAKrYyH/Heclgn+JtizazweGd9IzzWjrefCQbKQogyQwKBgDL2v9d4XNa9u5Jwq/T42D0augJIF//7Mp3LXlTramQ8/O+U3syeft0vC2ZkpBSyPztSBfuVsZg/ux1PGmMaFbYTW6XP9l8SZ3czcr1OUo44OzaOxatbuXodaa8zUuC4ckvnOXozAA9ZBaM7SuV270tMXho3wYbHwRi0wlXTK0WRAoGAcfe/pxa+Fv7+IC7Zh52a3cGnvukOle+l0NLfD1yqq6YRvq3QIPBoAjD/8wfu4LF7/7EOYJ49dvqSuFX6aDVO5oyrjUV75jxMjOMTuJb4M8TqvRA7g5vjfw02kpwk9FNtSjkIRqRSy4J0Vs5FQAFJBVAAPGskWGsmt+S/iUG3Ki8CgYAD+8j0gSopXb2bzjF/3rcDasf+Nix89Tv6xjuaT4BpB/hwkPyI0Jvwm6JfF+GzzXKJqLXqOQ4dl8iycKjezvyInaMUudqAKVLrcURVfyJmtUbNdMfRVtDEf/o5+6nKaBPkc5hmbL796jJes/fM8MORmvRTEYaxvAHqQnS5klvFWQ==";
        Map<String, String> otherMap = new HashMap<>();
        otherMap.put(PARTNER_ID, partnerId);
        otherMap.put(MCMS_DATE, mcmsDate);
        otherMap.put(SIGN_TYPE, signType);
        String encryptionData = PospVerifyUtils.getEncryptionDate(null, request.getQueryString(), otherMap);
        System.out.println(encryptionData);
        return Base64.encodeBase64String(RSAUtils.sign(RSAUtils.loadPrivateKey(keyEntity), encryptionData.getBytes("UTF-8"), RSAUtils.SIGNATURE_ALGORITHM_SHA256WITHRSA));
    }

    @RequestMapping(value = "/testPush1", method = RequestMethod.POST)
    @ResponseBody
    public Object testPush1(HttpServletRequest request, @RequestBody PushEntity pushEntity){
        String token = request.getHeader("token");
        System.out.println(token);
        System.out.println(pushEntity.toString());
        return "success";
    }

    @RequestMapping(value = "/testPush", method = RequestMethod.POST)
    @ResponseBody
    public String testPush(HttpServletRequest request, @RequestBody CouponExchangeNotifyEntity couponExchangeNotifyEntity){
        System.out.println(couponExchangeNotifyEntity.toString());
        return "success";
    }

}